#
# 【解析器】
#
# bs4 模块需要安装
# sudo pip3 install bs4
#
# 对外提供一个方法：
#
# 1. parser(url)
# BeautifulSoup DOM 树处理，接收两个参数 URL 和 html 内容
#
# 对内提供两个方法：
#
# 1. _get_urls(url, soup)
# 获取网页中的 URL，接收两个参数 URL 和 DOM 树
#
# 2. _get_data(url, soup)
# 获取网页中的数据，接收两个参数 URL 和 DOM 树
#
from abc import abstractmethod

from bs4 import BeautifulSoup


class Parser(object):
    def __init__(self):
        super().__init__()

    def parser(self, url, html):
        # 判断 url 是否为空，或网页数据是否为空，为空则返回 None
        if url is None or html is None:
            return
        # 格式化 html 为 BeautifulSoup DOM 树
        # soup = BeautifulSoup(html, features='html.parser')
        soup = BeautifulSoup(html, features="lxml")
        # soup = BeautifulSoup(html)
        # 获取网页中的 URL
        urls = self.parse_url(url, soup)
        # 获取网页中的数据
        data = self.parse_data(url, soup)
        return urls, data

    @staticmethod
    def _parse_url_demo(url, soup):
        urls = set()
        links = soup.select(r'body > div.w1180 > div.maincontent > div.page-content > div > a.aNxt', )
        if links:
            for link in links:
                new_url = link.attrs["href"]
                urls.add(new_url)
            return urls

    @staticmethod
    def _parse_data_demo(url, soup) -> list:
        house_name_list = soup.select(r'#list-content div.li-info > h3 > a', )
        house_area_address_list = soup.select("#list-content  div.li-info > address", )
        result_list = []
        for i in range(len(house_name_list)):
            house_name = house_name_list[i].text
            house_area_address = house_area_address_list[i].text
            area_address = house_area_address.split("]")
            house_address = area_address[1].strip()
            house_areas = area_address[0].strip().split("-")
            house_area = house_areas[0].strip()[1:]
            house_area2 = house_areas[1].strip()
            result_list.append([house_name, house_area, house_area2, house_address])
        return result_list

    @abstractmethod
    def parse_url(self, url, soup) -> list:
        pass

    @abstractmethod
    def parse_data(self, url, soup) -> list:
        pass
